package com.ucombuy.youdiangou.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by yaosheng on 2021/12/20.
 */
@Configuration
public class ElasticsearchRestClient {

    @Value ("${elasticsearch.ip}")
    private String ipAddress;

    @Bean(name = "highLevelClient")
    public RestHighLevelClient highLevelClient(){

        String[] address = ipAddress.split (":");
        String ip = address[0];
        int port = Integer.valueOf (address[1]);
        HttpHost httpHost = new HttpHost (ip,port,"http");

        return new RestHighLevelClient (RestClient.builder (new HttpHost[]{httpHost}));
    }
}
